package com.zimi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.model.WfForm;
import com.zimi.service.WfFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: lmc
 * @Date: 2019/3/27 10:34
 * @Description:
 */

@Api("流程模块")
@RestController
@RequestMapping("wf/form")
public class WfFormApi {

    @Autowired
    private WfFormService wfFormService;


    @PostMapping("pageList")
    @ApiOperation("分页查询")
    public Map pageList(WfForm wfForm) {
        PageHelper.startPage(wfForm.getPage(), wfForm.getLimit());
        List<WfForm> list = wfFormService.selectAll(wfForm);
        PageInfo<WfForm> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("OK", "OK");
        map.put("code", 200);
        map.put("total", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }

    @PostMapping("list")
    @ApiOperation("查询所有")
    public Map getAll(WfForm wfForm) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        returnMap.put("rows", wfFormService.selectAll(wfForm));
        return returnMap;
    }

    @PostMapping("findById")
    @ApiOperation("根据Id查询信息")
    public Map selectByPrimaryKey(int id) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        returnMap.put("wfForm", wfFormService.selectByPrimaryKey(id));
        return returnMap;
    }

    @PostMapping("deleteById")
    @ApiOperation("根据Id删除")
    public Map deleteByPrimaryKey(int id) {
        Map returnMap = new HashMap();
        boolean flag = wfFormService.deleteByPrimaryKey(id) > 0 ? true : false;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "400");
        }
        return returnMap;
    }

    @PostMapping("insert")
    @ApiOperation("添加")
    public Map insert(WfForm wfForm) {
        boolean flag = wfFormService.insertSelective(wfForm) > 0 ? true : false;
        Map returnMap = new HashMap();
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "500");
        }
        return returnMap;
    }

    @PostMapping("update")
    @ApiOperation("根据条件修改信息")
    public Map updateByPrimaryKey(WfForm wfForm) {
        boolean flag = wfFormService.updateByPrimaryKeySelective(wfForm) > 0 ? true : false;
        Map returnMap = new HashMap();
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {

            returnMap.put("OK", "error");
            returnMap.put("code", "500");
        }
        return returnMap;

    }


    @PostMapping("moveUp")
    @ApiOperation("上移")
    public Map moveUp(int id) {
        Map returnMap = new HashMap();
        returnMap.put("msg", wfFormService.moveUp(id));
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        return returnMap;
    }

    @PostMapping("moveDown")
    @ApiOperation("上移")
    public Map moveDown(int id) {
        Map returnMap = new HashMap();
        returnMap.put("msg", wfFormService.moveDown(id));
        returnMap.put("OK", "OK");
        returnMap.put("code", "200");
        return returnMap;
    }

    @PostMapping("saveFormById")
    @ApiOperation("上移")
    public Map saveFormById(int id) {
        Map returnMap = new HashMap();
        WfForm wfForm = wfFormService.selectByPrimaryKey(id);
        wfForm.setFormId(null);
        String dis = (wfForm.getDisplayName()) + "备份";
        wfForm.setDisplayName(dis);
        boolean flag = wfFormService.insertSelective(wfForm) > 0 ? true : false;
        if (flag) {
            returnMap.put("OK", "OK");
            returnMap.put("code", "200");
        } else {
            returnMap.put("OK", "error");
            returnMap.put("code", "500");
        }
        return returnMap;
    }

}
